//
//  JSViewController04.swift
//  RXSwiftTest
//
//  Created by ZhangBo on 2019/6/19.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class JSViewController04: BaseViewController {
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        test01()
        test02()
        test03()
        test04()
        test05()
    }
    
}

extension JSViewController04 {
    fileprivate func test01() {
        let observable = Observable.of("A", "B", "C")
        observable.subscribe { event in
            print(event)
        }.disposed(by: disposeBag)
    }
    
    fileprivate func test02() {
        let observable = Observable.of("A", "B", "C")
        
        observable.subscribe { event in
            print(event.element ?? "nil")
        }.disposed(by: disposeBag)
    }
    
    fileprivate func test03() {
        let observable = Observable.of("A", "B", "C")
        
        observable.subscribe(onNext: { element in
            print(element)
        }, onError: { error in
            print(error)
        }, onCompleted: {
            print("completed")
        }, onDisposed: {
            print("disposed")
        }).disposed(by: disposeBag)
    }
    
    
    fileprivate func test04() {
        let observable = Observable.of("A", "B", "C")
        
        observable.do(onNext: { element in
            print("Intercepted Next：", element)
        }, onError: { error in
            print("Intercepted Error：", error)
        }, onCompleted: {
            print("Intercepted Completed")
        }, onDispose: {
            print("Intercepted Disposed")
        })
            .subscribe(onNext: { element in
                print(element)
            }, onError: { error in
                print(error)
            }, onCompleted: {
                print("completed")
            }, onDisposed: {
                print("disposed")
            }).disposed(by: disposeBag)
    }
    
    
    fileprivate func test05() {
        
        //第1个Observable，及其订阅
        let observable1 = Observable.of("A", "B", "C")
        observable1.subscribe { event in
            print(event)
            }.disposed(by: disposeBag)
        
        //第2个Observable，及其订阅
        let observable2 = Observable.of(1, 2, 3)
        observable2.subscribe { event in
            print(event)
            }.disposed(by: disposeBag)
    }
    
}
